//
//  QZShopViewController.m
//  WWShop
//
//  Created by 超群 on 15/10/14.
//  Copyright © 2015年 qunzhu.Team. All rights reserved.
//

#import "QZShopViewController.h"
#import "QZShowScrollCell.h"
#import "MyButton.h"
#import "QZShopListController.h"
#import "QZShowCatagaryCell.h"
#import "MyButton.h"
#import "QZHairDetailViewController.h"

#import "QZShopCategory.h"

#import "ListeView.h"


@interface QZShopViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>


@property (nonatomic, strong) UITableView *mainVeiw;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageView;
@property (weak, nonatomic) IBOutlet UIView *categarys;

@property (nonatomic, strong) UITextField *searchBar;

@property (nonatomic, strong) UIView *gayView;

@property (nonatomic, strong) ListeView *shopListe;


@property (nonatomic, strong) NSMutableArray *shopCategorys;


@end

@implementation QZShopViewController{
    
    UIView *_leftView;
    
}

-(NSMutableArray *)shopCategorys{
    if (_shopCategorys == nil) {
        _shopCategorys = [[NSMutableArray alloc] init];
    }
    return  _shopCategorys;
 
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    //创建NavigationBar 上的按钮
    [self prepreNvcButton];
    //创建tableView
    [self createTableView];
    //创建蒙版，当点击搜索的时候有个蒙版，可以透明
    [self createGayView];
    
    [self createShopCategory];
    //获取分类列表
    [self downloadData];

}
#pragma mark 隐藏 键盘
- (void)hideKeyboard:(UITapGestureRecognizer *)tap
{
    _gayView.hidden = YES;
    [_shopListe lisetViewHieddn];
    [self.searchBar resignFirstResponder];
}
#pragma mark 创建 navigationBar
-(void)prepreNvcButton{
    //左边的item
//    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftClick:)];
//    
//    UIView *left = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 60, 30)];
//    _leftView = left;
//    [left addGestureRecognizer:tapLeft];
//    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
//    lable.text = @"商品";
//    lable.textColor = [UIColor whiteColor];
//    [left addSubview:lable];
//    
//    UIImageView *view =[[UIImageView alloc] initWithFrame:CGRectMake(40, 10, 15, 10)];
//    view.image = [UIImage imageNamed:@"矩形-18"];
//    [left addSubview:view];
//    
//    UIBarButtonItem *itme =[[UIBarButtonItem alloc] initWithCustomView:left];
//    self.navigationItem.leftBarButtonItem =itme;
    
    
    //添加搜索框
    UIView *search = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W-30, 30)];
   
    search.layer.masksToBounds = YES;
    search.layer.cornerRadius = 15;
    
    search.backgroundColor = RGBACOLOR(255, 255, 255, 0.4);

    _searchBar = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, SCREEN_W-30, 30)];
    _searchBar.returnKeyType = UIReturnKeySearch;
    _searchBar.placeholder = @"搜索商品";
    _searchBar.backgroundColor = [UIColor clearColor];
    _searchBar.delegate = self;
    
    [search addSubview:_searchBar];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchButtonClick:)];
    
    UIImageView *searchImage = [[UIImageView alloc] initWithFrame:CGRectMake(_searchBar.frame.size.width-30, 5, 20, 20)];
    searchImage.userInteractionEnabled = YES;
    [searchImage addGestureRecognizer:tap];
    searchImage.image = [UIImage imageNamed:@"查找"];
    [search addSubview:searchImage];
    
    [self.navigationItem setTitleView:search];

}

-(void)leftClick:(UITapGestureRecognizer *)tap{
    static BOOL isSelect;
    if (!isSelect) {
        [_shopListe lisetViewShow];
        _gayView.hidden = NO;
        
    }else{
        [_shopListe lisetViewHieddn];
        _gayView.hidden = YES;
    }
    isSelect = !isSelect;


}
/**
 *  搜索的按钮，点击事件
 *
 *  @param tap tap
 */
#pragma mark 搜索点击按钮
-(void)searchButtonClick:(UITapGestureRecognizer *)tap{
    _gayView.hidden = YES;
    [_shopListe lisetViewHieddn];
    [_searchBar resignFirstResponder];
    NSLog(@"搜索了");
    [self seachNewList];


}

#pragma mark textField delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    _gayView.hidden = NO;
    [_shopListe lisetViewHieddn];
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    _gayView.hidden = YES;
    [_shopListe lisetViewHieddn];
    
    [self seachNewList];
    
    return YES;
}

-(void)seachNewList{
    
    if (_searchBar.text.length > 0) {
         
        QZShopListController *listVc = [[QZShopListController alloc] init];
        
        listVc.searchString = _searchBar.text;
        listVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:listVc animated:YES];
    }else{
        [[iToast makeText:@"请输入搜索条件"] show];
    }
    
    _searchBar.text = @"";
}


- (void)createTableView{

    _mainVeiw = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-49)style:UITableViewStylePlain];
    _mainVeiw.dataSource = self;
    _mainVeiw.delegate = self;
    _mainVeiw.separatorStyle = NO;
    [self.view addSubview:_mainVeiw];

}

#pragma mark tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"showScroll";
    static NSString *cellID2= @"buttons";
    QZShowScrollCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    QZShowCatagaryCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellID2];
    
    
    if (cell == nil) {
        cell = [[QZShowScrollCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        //点击scrollView 跳转
        cell.block = ^(id objct){
//            QZHairDetailViewController *deta = [[QZHairDetailViewController alloc] initWithNibName:nil bundle:nil];
//            deta.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:deta animated:YES];
        };

    }
    
    
    if (cell2 == nil) {
        cell2 = [[QZShowCatagaryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell2.catagaryBlock= ^(id objct){
            // 点击事件传过来的Model  直接能用
            QZShopCategory *model = objct;
            
            QZShopListController *controller = [[QZShopListController alloc] init];
            controller.prod_cate_id = model.proTypeId;
            controller.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:controller animated:YES];
            
        };

    }
    if (indexPath.row == 0) {
        
        return cell;
    }
    if (indexPath.row == 1) {
        cell2.categarys = _shopCategorys;
        return cell2;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        
        return 280;
    }else {
        CGFloat gay = 35;
        CGFloat buttonW = (SCREEN_W-4*gay)/3;
        CGFloat buttonH =buttonW + gay;
        
        
      return ((_shopCategorys.count-1)/3+1)*buttonH + gay;
        
    }
    
}


-(void)createGayView{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
    
    _gayView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64)];
    _gayView.hidden = YES;
    _gayView.backgroundColor = [UIColor blackColor];
    _gayView.alpha = 0.3;
    [_gayView addGestureRecognizer:tap];
    [self.navigationController.tabBarController.view addSubview:_gayView];
    [self.navigationController.tabBarController.view bringSubviewToFront:_gayView];
//    [self.view addSubview:_gayView];
}

-(void)createShopCategory{
    
    _shopListe = [[ListeView alloc] initWithFrame:CGRectMake(20,52, 100, 200)];

    
    [self.navigationController.tabBarController.view addSubview:_shopListe];

    _shopListe.frame = CGRectMake(20, 50, 100, 0);
    
    
}

-(void)downloadData{
    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"Prod.GetProdCate"];
    
    [self.request QZRequest_POST:url parameters:nil tagNSString:@"商品分类" stopRequest:YES isSerializer:NO isCache:YES];

}

-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag{
    if ([value class] != NSClassFromString(@"__NSCFDictionary")) {
        value = [NSJSONSerialization JSONObjectWithData:value options:NSJSONReadingAllowFragments error:nil];
    }
    
    
    if ([tag isEqualToString:@"商品分类"]) {
        
        NSArray *data = value[@"data"][@"data"][@"list"];
        if ([data isKindOfClass:[NSNull class]]) {
            
            [[iToast makeText:@"没有分类"] show];
            return;
        }
        
        for (NSDictionary *dic in data) {
            QZShopCategory *model = [[QZShopCategory alloc] initWithDictionary:dic error:nil];
            [self.shopCategorys addObject:model];
        }
        _shopListe.datas = self.shopCategorys;
        [_mainVeiw reloadData];
    }

}



@end
